package com.net.xpay.web.controller.open;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.xpay.common.domain.UserShard;
import com.net.xpay.common.manager.UserShardManager;
import com.net.xpay.core.domain.ChargeRecord;
import com.net.xpay.core.domain.PayTransBatch;
import com.net.xpay.core.domain.PayTransSummary;
import com.net.xpay.core.manager.ChargeRecordManager;
import com.net.xpay.core.manager.PayTransBatchManager;
import com.net.xpay.core.manager.PayTransSummaryManager;
import com.net.shard.ShardUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;

/**
 * 2020/6/28 09:46

 */
@Controller
@Slf4j
@RequestMapping("/open/pay-trans")
@Api(tags = "第三方开放调用/批量打款", description = "第三方开放调用 批量打款 net")
public class OpenPayTransController {



    @Autowired
    private UserShardManager userShardManager;

    @Autowired
    private PayTransBatchManager payTransBatchManager;

    @Autowired
    private PayTransSummaryManager payTransSummaryManager;


    @Autowired
    private ChargeRecordManager chargeRecordManager;



    /**
     * 移动端充值页面
     */
    @ApiOperation("移动端充值页面")
    @GetMapping("/charge-page/{outBizNo}")
    public void goMobileChargePage(HttpServletResponse response, @PathVariable("outBizNo") String outBizNo) {
        try {
            Preconditions.checkArgument(!Strings.isNullOrEmpty(outBizNo));
            String[] arr = outBizNo.split("_");
            Long userId = Long.valueOf(arr[0]);
            Long id = Long.valueOf(arr[1]);

            UserShard userShard = userShardManager.getById(userId);
            ShardUtil.setShardContext(userShard.getShardId());
            ChargeRecord chargeRecord = chargeRecordManager.getById(id);
            response.setHeader("Content-Type","text/html; charset=utf-8");
            response.getWriter().println("<!DOCTYPE html>\n" +
                    "<html lang=\"\">\n" +
                    "  <head>\n" +
                    "    <meta charset=\"utf-8\">\n" +
                    "  </head>\n" +
                    "  <body>\n" +
                    "      <script>\n" +
                    "          window.location.href = '"  + chargeRecord.getLastMobilePayUrl() +  "';\n" +
                    "      </script>\n" +
                    "  </body>\n" +
                    "</html>\n");
            response.flushBuffer();
        } catch (Exception e) {
            log.error("goMobileChargePage error", e);
        } finally {
            ShardUtil.cleanShardContext();
        }

    }

    /**
     * 移动端支付页面
     */
    @ApiOperation("移动端支付页面")
    @GetMapping("/pay-page/{outBatchNo}")
    public void goMobilePayPage(HttpServletResponse response, @PathVariable("outBatchNo") String outBatchNo) {
        try {
            Preconditions.checkArgument(!Strings.isNullOrEmpty(outBatchNo));
            String[] arr = outBatchNo.split("-");
            Long userId = Long.valueOf(arr[0]);
            Long summaryId = Long.valueOf(arr[1]);

            UserShard userShard = userShardManager.getById(userId);
            ShardUtil.setShardContext(userShard.getShardId());
            PayTransSummary payTransSummary = payTransSummaryManager.getById(summaryId);
            PayTransBatch batch = payTransBatchManager.getById(payTransSummary.getPayTransBatchId());
            response.setHeader("Content-Type","text/html; charset=utf-8");
            response.getWriter().println("<!DOCTYPE html>\n" +
                    "<html lang=\"\">\n" +
                    "  <head>\n" +
                    "    <meta charset=\"utf-8\">\n" +
                    "  </head>\n" +
                    "  <body>\n" +
                    "      <script>\n" +
                    "          window.location.href = '"  + batch.getLastMobilePayUrl() +  "';\n" +
                    "      </script>\n" +
                    "  </body>\n" +
                    "</html>\n");
            response.flushBuffer();
        } catch (Exception e) {
            log.error("goMobilePayPage error", e);
        } finally {
            ShardUtil.cleanShardContext();
        }
    }


}
